package com.aistar.controller;

import com.aistar.pojo.ClubActivity;
import com.aistar.pojo.Member;
import com.aistar.service.ClubListService;
import com.aistar.service.MemberService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
public class ClubListController {

    @Autowired
    private ClubListService clubListService;
    @Autowired
    private MemberService memberService;

//    //转发到jsp
//    @GetMapping("/clublist")
//    public String clublist(){
//        return "clublist.jsp";
//    }

    @GetMapping("/clublist/list")
    public ModelAndView list(HttpServletRequest request){
        String pageNumStr = request.getParameter("page");
        System.out.println("pageNumStr =" + pageNumStr);
        Integer pageNum = null;
        if(pageNumStr == null)
            pageNum=1;
        else
            pageNum=Integer.valueOf(pageNumStr);
        //service 调用
        PageInfo<ClubActivity> pageInfo = clubListService.getAll(pageNum,6);
        System.out.println("pageInfo =" + pageInfo.getList());
        //数据绑定
        List<ClubActivity> clubActivityList = pageInfo.getList();
        System.out.println("俱乐部列表信息：" + clubActivityList);

        Object ss = request.getSession().getAttribute("userId");//获得session里的用户id
        int userId = Integer.parseInt(String.valueOf(ss));
        Member member = memberService.getbyId(userId);

        ModelAndView mav = new ModelAndView();
        mav.addObject("memberall",member);

        mav.addObject("clubActivityList",clubActivityList);
        mav.addObject("page",pageInfo);
        mav.setViewName("ClubList/clublist");
        return mav;
    }

}
